#include "stdafx.h"
#include "ParamShare.h"
#include "dencode.h"

void CParamShareClient::readINI(LPCWSTR section, CString preKey, const CINIOperater *oper)
{
	svrBidno = oper->GetString(section, preKey + CString(L"\\SvrBidno"), L"");
	svrPassword = oper->GetString(section, preKey + CString(L"\\SvrPassword"), L"");
}
void CParamShareClient::writeINI(LPCWSTR section, CString preKey, CINIOperater *oper) const
{
	oper->WriteString(section, preKey + CString(L"\\SvrBidno"), svrBidno.IsEmpty() ? nullptr : (LPCWSTR)svrBidno);
	oper->WriteString(section, preKey + CString(L"\\SvrPassword"), svrPassword.IsEmpty() ? nullptr : (LPCWSTR)svrPassword);
}
bool CParamShareClient::isEqual(const CParamShareClient &p) const
{
	return svrBidno == p.svrBidno &&
		svrPassword == p.svrPassword;
}



void CParamShareServer::readINI(LPCWSTR section, CString preKey, const CINIOperater *oper)
{
	password = oper->GetString(section, preKey + CString(L"\\Password"), L"");
	allowAllUser = oper->GetBoolean(section, preKey + CString(L"\\AllowAllUser"), false);
	CString user = oper->GetString(section, preKey + CString(L"\\User"), L"");
	if (user.IsEmpty())
		allowUser.clear();
	else
		allowUser.build(user, ';');
}
void CParamShareServer::writeINI(LPCWSTR section, CString preKey, CINIOperater *oper) const
{
	oper->WriteString(section, preKey + CString(L"\\Password"), password.IsEmpty() ? nullptr : (LPCWSTR)password);
	oper->WriteBoolean(section, preKey + CString(L"\\AllowAllUser"), allowAllUser);
	LPCWSTR user = getUserString();
	oper->WriteString(section, preKey + CString(L"\\User"), user);
	if (user) HeapFree(GetProcessHeap(), 0, (LPVOID)user);
}
bool CParamShareServer::isEqual(const CParamShareServer &p) const
{
	if (password != p.password || allowAllUser != p.allowAllUser || allowUser.count != p.allowUser.count) return false;
	for (intptr_t i = 0; i < allowUser.count; i++) if (wcscmp(allowUser.str[i], p.allowUser.str[i])) return false;
	return true;
}
LPCWSTR CParamShareServer::getUserString() const
{
	if (allowUser.count <= 0) return nullptr;
	CString user = L"";
	for (intptr_t i = 0; i < allowUser.count; i++)
	{
		if (!user.IsEmpty()) user += L";";
		user += allowUser.str[i];
	}
	return copyStr(user);
}







CParamShare::CParamShare() :CParameter(L"Share")
{
}
void CParamShare::readINI(const CINIOperater *oper)
{
	client.readINI(section, regParm.preKey() + L"Client", oper);
	server.readINI(section, regParm.preKey() + L"Server", oper);
}
void CParamShare::writeINI(CINIOperater *oper) const
{
	client.writeINI(section, regParm.preKey() + L"Client", oper);
	server.writeINI(section, regParm.preKey() + L"Server", oper);
}
bool CParamShare::isEqual(const CParamShare &p) const
{
	return client.isEqual(p.client) &&
		server.isEqual(p.server);
}
